package com.springboot.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect // 定义一个切面
@Component // 将该类声明为一个Spring组件
public class TestAop {

    //    @Pointcut("execution(* com.springboot.controller.*.*(..))") // 定义一个切点
    @Pointcut("@annotation(com.springboot.aop.ControllerAop)") // 定义一个切点
    public void ControllerCut() {

    }


    // 前置通知
    @Before("ControllerCut()")
    public void beforeAdvice(JoinPoint joinPoint) {
        System.out.println("before前置通知");
    }

    @After("ControllerCut()")
    public void afterAdvice(JoinPoint joinPoint) {
        System.out.println("After后置通知");
    }


    @Around("ControllerCut()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) {
        System.out.println("Around环绕通知");
        Object proceed = null;
        try {
            System.out.println("Around进入方法");
            proceed = joinPoint.proceed();
            System.out.println("Around退出方法");
        } catch (Exception exception) {
            proceed = exception.getMessage() ;
        } finally {
            return proceed;
        }
    }
}